a = int(input())
b = 2; c = 0; d = 0; e = 0; f = 0 ; ed = [] ;des = [] ;sot = [] ; chisl_1 = 0 ; j = 1 ; z = 0;chisl_2 = 0;chisl_3 = 0 ; chisl = 0
if a < 999 or a > 10000 :
    print(0)
else:
    f = a % 10
    e = a // 10 % 10
    d = a // 100 % 10
    c = a // 1000
    ed.append(f)
    ed.append(e)
    ed.append(d)
    ed.append(c)
    
    h = (c * 10) + d 
    s=(d * 10) + e 
    k =(e * 10) + f
    des.append(h)
    des.append(s)
    des.append(k)
    
    q = (c * 100) + (d * 10) + e
    w = (d * 100) + (e * 10) + f
    sot.append(q)
    sot.append(w)
    
    k = 0
    
    for l in range(2, a // 2+1):
        if a % l == 0:
            k = k+1
    if k <= 0:
        chisl = a
    
    for i in range(4):
        k = 0
        for j in range(2, ed[i] // 2+1):
            if ed[i] % j == 0:
                k = k+1
        if k <= 0:
            if ed[1] > chisl_1:
                chisl_1 = ed[i]
        i += 1
        j += 1
    
    
    for p in range(3):
        k = 0
        for z in range(2, des[p] // 2+1):
            if des[p] % z == 0:
                k = k+1
        if k <= 0:
            if des[p] > chisl_2:
                chisl_2 = des[p]
        p += 1
        z += 1
    
    for m in range(2):
        k = 0
        for u in range(2, sot[m] // 2+1):
            if sot[m] % u == 0:
                k = k+1
        if k <= 0:
            if sot[m] > chisl_3:
                chisl_3 = sot[m]
        m += 1
        u += 1
        
    if chisl > chisl_1 and chisl > chisl_2 and chisl > chisl_3:
        print(chisl)
    if  chisl_1 > chisl and chisl_1 > chisl_2 and chisl_1 > chisl_3 :
        print(chisl_1)
    if  chisl_2 > chisl and chisl_2 > chisl_1 and chisl_2 > chisl_3:
        print(chisl_2)
    if  chisl_3 > chisl and chisl_3 > chisl_1 and chisl_3 > chisl_2:
        print(chisl_3)
    if chisl == 0 and chisl_1 == 0 and chisl_2 == 0 and chisl_3 == 0 :
        print(0)